package com.qf.mywork.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSession_utils {
    public static SqlSession getSqlsession() {
        // 把mybatis-config.xml转换成输入流对象
        InputStream is = null;
        SqlSession sqlSession = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            // 创建一个 SqlSessionFactoryBuilder对象
            SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();

            // 利用 SqlSessionFactory 对象可以创建 Sqlsession对象
            SqlSessionFactory factory = sfb.build(is);

            // 核心对象 SqlSession
            sqlSession = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sqlSession;
    }
}
